using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Xml;

namespace blogapi
{
    public partial class publy : Form
    {
        public publy(pages pg,nastaveni nas)
        {
            InitializeComponent();
            pag = pg;
            nastav = nas;

            prispevkypole = new ArrayList();
            if (nastav.typzapisu == 0)
            {
                if (File.Exists(nastav.nachazitese+"/soubory.db"))
                {
                    bool dal = true;
                    FileStream fs = null;
                    BinaryReader ct = null;
                    try
                    {
                        fs = new FileStream(nastav.nachazitese + "/soubory.db", FileMode.Open);
                        ct = new BinaryReader(fs, Encoding.UTF8);
                    }
                    catch
                    {
                        dal = false;
                    }
                    if (dal)
                    {
                        try
                        {
                            while (true)
                            {
                                prispevek pris = new prispevek(ct.ReadString(), ct.ReadString(), ct.ReadInt64(),ct.ReadString());
                                prispevkypole.Add(pris);
                            }
                        }
                        catch
                        {
                        }
                        fs.Close();
                        ct.Close();
                    }
                }
            }
            else
            {
                if (File.Exists(nastav.nachazitese + "/data.xml"))
                {
                    XmlTextReader cti = null;
                    try
                    {
                        cti = new XmlTextReader(nastav.nachazitese + "/data.xml");
                        while (!cti.EOF)
                        {
                            if (cti.MoveToContent() == XmlNodeType.Element && cti.Name == "posts")
                            {
                                while (!cti.EOF)
                                {
                                    if (cti.MoveToContent() == XmlNodeType.Element && cti.Name == "post")
                                    {
                                        string tit = "";
                                        string txt = "";
                                        string kat = "";
                                        Int64 date = 0;
                                        while (!cti.EOF)
                                        {
                                            if (cti.MoveToContent() == XmlNodeType.Element && cti.Name == "categories")
                                            {
                                                kat = cti.ReadElementContentAsString();
                                                continue;
                                            }
                                            if (cti.MoveToContent() == XmlNodeType.Element && cti.Name == "title")
                                            {
                                                tit = cti.ReadElementContentAsString();
                                                continue;
                                            }
                                            if (cti.MoveToContent() == XmlNodeType.Element && cti.Name == "text")
                                            {
                                                txt = cti.ReadElementContentAsString();
                                                continue;
                                            }
                                            if (cti.MoveToContent() == XmlNodeType.Element && cti.Name == "date")
                                            {
                                                date = cti.ReadElementContentAsLong();
                                                continue;
                                            }
                                            if (cti.MoveToContent() == XmlNodeType.EndElement && cti.Name == "post")
                                            {
                                                break;
                                            }
                                            cti.Read();
                                        }
                                        prispevek pris = new prispevek(tit, txt, date,kat);
                                        prispevkypole.Add(pris);
                                        continue;
                                    }
                                    if (cti.MoveToContent() == XmlNodeType.EndElement && cti.Name == "posts")
                                    {
                                        break;
                                    }
                                    cti.Read();
                                }
                                break;
                            }
                            else
                                cti.Read();
                        }
                        cti.Close();
                    }
                    catch
                    {
                    }
                    if (cti != null)
                    {
                        cti.Close();
                    }
                }
            }

            vypis(true);
            sr = new servery(this, pag, nastav);
            setlang();
        }
        public ArrayList prispevkypole;
        pages pag;
        nastaveni nastav;

        public void vypis(bool oznaceni)
        {
            listView1.Items.Clear();
            for (int i = 0; i < prispevkypole.Count; i++)
            {
                prispevek pris = (prispevek)prispevkypole[i];
                ListViewItem list = new ListViewItem(pris.tabulka());
                list.Tag = i;
                list.Checked = oznaceni;
                listView1.Items.Add(list);
            }
        }

        servery sr;
        private void menuItem2_Click(object sender, EventArgs e)
        {
            bool projdu = false;
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                if (listView1.Items[i].Checked==true)
                {
                    projdu = true;
                    break;
                }
            }
            if (!projdu)
            {
                MessageBox.Show(nastav.getlang("publikacevybrat"),nastav.getlang("publikacepriss"));
                return;
            }
            sr.nacti();
            sr.Show();
        }


        private void menuItem5_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedIndices.Count != 0)
            {
                novyprispevek nov = new novyprispevek(this,nastav,pag);
                nov.nastav((int)listView1.Items[listView1.SelectedIndices[0]].Tag,nastav.autobr);
                nov.Show();
            }
            else
            {
                MessageBox.Show(nastav.getlang("chosepolozka"), nastav.getlang("spatneoznac"));
            }
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            if (DialogResult.No == MessageBox.Show(nastav.getlang("smazatprisp"), nastav.getlang("smazanipris"), MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1))
            {
                return;
            }
            for (int i = listView1.Items.Count-1; i >=0; i--)
            {
                if (!listView1.Items[i].Checked)
                {
                    continue;
                }
                int id = (int)listView1.Items[i].Tag;
                prispevkypole.RemoveAt(id);
            }
            vypis(true);
        }

        private void menuItem7_Click(object sender, EventArgs e)
        {
            novyprispevek nov = new novyprispevek(this,nastav,pag);
            nov.nastav(-1,nastav.autobr);
            nov.Show();
        }

        private void menuItem9_Click(object sender, EventArgs e)
        {
            vypis(true);
        }

        private void menuItem10_Click(object sender, EventArgs e)
        {
            vypis(false);
        }

        public void setlang()
        {
            sr.setlang();
            menuItem1.Text = nastav.getlang("moznosti");
            menuItem5.Text = nastav.getlang("zpet");
            menuItem7.Text = nastav.getlang("novy");
            menuItem3.Text = nastav.getlang("upravit");
            menuItem4.Text = nastav.getlang("smazat");
            menuItem9.Text = nastav.getlang("oznacitvse");
            menuItem2.Text = nastav.getlang("publikovat");
            label1.Text = nastav.getlang("prispevkyvyber");
            columnHeader1.Text = nastav.getlang("nazev");
            columnHeader2.Text = nastav.getlang("text");
            columnHeader3.Text = nastav.getlang("datum");
            menuItem10.Text = nastav.getlang("odznacit");
            Text = "W - " + nastav.getlang("publikace");
        }
    }
    class prispevek
    {
        public string text;
        public string titulek;
        public DateTime datum;
        public string[] kategorie;
        public prispevek(string tit,string txt, Int64 dat, string kat)
        {
            titulek = tit;
            text = txt;
            datum = new DateTime(dat);
            kategorie = kat.Split(',');
        }
        public string kate()
        {
            string vys = "";
            for (int i = 0; i < kategorie.Length; i++)
            {
                if (kategorie[i] == "")
                {
                    continue;
                }
                if (vys != "")
                {
                    vys += ",";
                }
                vys += kategorie[i];
            }
            return vys;
        }
        public string[] tabulka()
        {
            string[] str = new string[3];
            str[0] = titulek;
            str[1] = text;
            str[2] = datum.ToShortDateString();
            return str;
        }
    }
}